package org.forum.chat.serverpoint.Message;

import javax.websocket.EncodeException;
import javax.websocket.Encoder;
import javax.websocket.EndpointConfig;

import org.json.JSONObject;

public class MessageEncoder implements Encoder.Text<Message>{

	@Override
	public void destroy() {
		
	}

	@Override
	public void init(EndpointConfig config) {
		
	}

	@Override
	public String encode(Message message) throws EncodeException {
		if(message instanceof ChatMessage){
			ChatMessage chatMessage = (ChatMessage)message;
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("type", Message.CHATMESSAGE);
			jsonObject.put("sender", chatMessage.getSender());
			jsonObject.put("to", chatMessage.getTo());
			jsonObject.put("content", chatMessage.getContent());
			return chatMessage.toString();
		}
		else if(message instanceof FindContactsMessage){
			
		}
		//退出信息
		else{
			
		}
		return null;
	}
	

}
